#-------------------------------------------------------#
#   Academic freedom and the onset of autocratization   #
#-------------------------------------------------------#

# Title: "Academic freedom and the onset of autocratization" #
# Authors: "Pelke, Lars", FAU Erlangen-Nürnberg
# date: 2023-04-24
# journal: Democratization
# DOI: 10.1080/13510347.2023.2207213
# written under "R version 4.2.3 (2023-03-15)"

#### Preliminaries ####

R.version$version.string

# clear workspace
rm(list=ls())

# set working directory
#setwd("P:/PIPM/7. VWS Projekt Academic Freedom Index/replication_files_autocratization_and_academic_freedom")

# loading packages

library(tidyverse)


#### Import Data ####

# Load VDem data
vdem <- readRDS("data/vdem11/Country_Year_V-Dem_Full+others_R_v11/V-Dem-CY-Full+Others-v11.rds")
vdem$iso3n <- countrycode(vdem$country_name, "country.name", "iso3n", warn = TRUE)
vdem$cowcode <- countrycode(vdem$country_name, "country.name", "cown", warn = TRUE)

vdem_full_v12 <- readRDS("data/vdem12/Country_Year_V-Dem_Full+others_R_v12/V-Dem-CY-Full+Others-v12.rds")
academic_freedom_four_data <-  readRDS("data/academic_freedom_index_cy.rds")

vdem_full_v12 <- vdem_full_v12 %>%
  dplyr::select(country_id, year, e_gdppc, e_pop)

vdem <- vdem %>%
  left_join(vdem_full_v12, by = c("country_id", "year"))
rm(vdem_full_v12)

vdem <- vdem %>%
  left_join(academic_freedom_four_data, by = c("country_text_id", "year")) %>%
  mutate(diff_v2xca_academ = v2xca_academ_four - v2xca_academ )

summary(vdem$diff_v2xca_academ)

vdem %>% 
  filter(diff_v2xca_academ >= 0.1 | diff_v2xca_academ <= -0.1) %>%
  dplyr::select(country_name, year, v2xca_academ, v2xca_academ_four,diff_v2xca_academ)

## Rename Variables ##

vdem <- vdem %>%
  rename(v2xca_academ_five = v2xca_academ, 
         v2xca_academ = v2xca_academ_four)
rm(academic_freedom_four_data)

#### Preparing VDem data ####
vdem <- vdem %>%
  dplyr::select(country_name,country_id, year, country_text_id, COWcode, cowcode, iso3n, v2x_polyarchy, v2xca_academ, v2xca_academ_five, 
                v2x_regime, e_gdppc, e_pop, e_regiongeo, e_regionpol)

vdem <- vdem %>%
  mutate(autocracy= ifelse(v2x_regime<2, 1, 0))


#####################################################################################################
#####################################################################################################

#### Change Socialization periods for former Soviet Unions and and Socialist Federal Republic of Yugoslavia ####

## gather information for USSR (saves as Russia in V-Dem) and then change country_id, country_name and iso3n for those entries ##

Armenia <- vdem %>%
  filter(country_name =="Russia" & year>=1936 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Armenia"), 
         country_id = case_when(country_id == 11 ~ 105), 
         iso3n = case_when(iso3n == 643 ~ 51))

Azerbaijan <- vdem %>%
  filter(country_name =="Russia" & year>=1936 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Azerbaijan"), 
         country_id = case_when(country_id == 11 ~ 106), 
         iso3n = case_when(iso3n == 643 ~ 31))

Belarus <- vdem %>%
  filter(country_name =="Russia" & year>=1945 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Belarus"), 
         country_id = case_when(country_id == 11 ~ 107), 
         iso3n = case_when(iso3n == 643 ~ 112))

BosniaHerzegovina <- vdem %>%
  filter(country_name =="Serbia" & year>=1945 & year <=1992) %>%
  mutate(country_name = case_when(country_name =="Serbia" ~ "Bosnia and Herzegovina"), 
         country_id = case_when(country_id == 198 ~ 150), 
         iso3n = case_when(iso3n == 688 ~ 70))

Estonia <- vdem %>%
  filter(country_name =="Russia" & year>=1944 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Estonia"), 
         country_id = case_when(country_id == 11 ~ 161), 
         iso3n = case_when(iso3n == 643 ~ 233))

Georgia <- vdem %>%
  filter(country_name =="Russia" & year>=1936 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Georgia"), 
         country_id = case_when(country_id == 11 ~ 118), 
         iso3n = case_when(iso3n == 643 ~ 268))

Latvia <- vdem %>%
  filter(country_name =="Russia" & year>=1944 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Latvia"), 
         country_id = case_when(country_id == 11 ~ 84), 
         iso3n = case_when(iso3n == 643 ~ 428))

Lithuania <- vdem %>%
  filter(country_name =="Russia" & year>=1944 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Lithuania"), 
         country_id = case_when(country_id == 11 ~ 173), 
         iso3n = case_when(iso3n == 643 ~ 440))

Moldova <- vdem %>%
  filter(country_name =="Russia" & year>=1944 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Moldova"), 
         country_id = case_when(country_id == 11 ~ 126), 
         iso3n = case_when(iso3n == 643 ~ 498))

NorthMacedonia <- vdem %>%
  filter(country_name =="Serbia" & year>=1912 & year <=1990) %>%
  mutate(country_name = case_when(country_name =="Serbia" ~ "North Macedonia"), 
         country_id = case_when(country_id == 198 ~ 176), 
         iso3n = case_when(iso3n == 688 ~ 807))

Slovenia <- vdem %>%
  filter(country_name =="Serbia" & year>=1945 & year <=1988) %>%
  mutate(country_name = case_when(country_name =="Serbia" ~ "Slovenia"), 
         country_id = case_when(country_id == 198 ~ 202), 
         iso3n = case_when(iso3n == 688 ~ 705))

Slovakia <- vdem %>%
  filter(country_name =="Czech Republic" & year>=1945 & year <=1992) %>%
  mutate(country_name = case_when(country_name =="Czech Republic" ~ "Slovakia"), 
         country_id = case_when(country_id == 157 ~ 202), 
         iso3n = case_when(iso3n == 203 ~ 705))

Ukraine <- vdem %>%
  filter(country_name =="Russia" & year>=1944 & year <=1989) %>%
  mutate(country_name = case_when(country_name =="Russia" ~ "Ukraine"), 
         country_id = case_when(country_id == 11 ~ 100), 
         iso3n = case_when(iso3n == 643 ~ 804))


vdem <- vdem %>%
  bind_rows(Armenia, Azerbaijan, Belarus, BosniaHerzegovina, Estonia, Georgia, Latvia, Lithuania, Moldova, 
            NorthMacedonia, Slovakia, Slovenia, Ukraine)

vdem <- vdem %>%
  group_by(country_name) %>%
  arrange(year)

## Short test ##

Moldova <- vdem %>%
  filter(country_name=="Moldova")

#remove additional datasets 

rm(Armenia, Azerbaijan, Belarus, BosniaHerzegovina, Estonia, Georgia, Latvia, Lithuania, Moldova, 
   NorthMacedonia, Slovakia, Slovenia, Ukraine)

#### Constructing Cohort Means for VDem ####

min.new <- function(v) {
  if (all(is.na(v))) { return(NA) } else { return(min(v, na.rm=T)) }
} 

max.new <- function(v) {
  if (all(is.na(v))) { return(NA) } else { return(max(v, na.rm=T)) }
}

mean.new <- function(v) {
  if (all(is.na(v))) { return(NA) } else { return(mean(v, na.rm=T)) }
}


vdem <- vdem %>% ungroup()

vdem_cohort_5 <- vdem %>%
  mutate(cohort_5 = cut(vdem$year, seq(1885, 2020, by = 5), right = F, labels = c(1885, 1890, 1895, 1900, 1905,
                                                                                  1910, 1915, 1920, 1925, 1930, 
                                                                                  1935, 1940, 1945, 1950, 1955,
                                                                                  1960, 1965, 1970, 1975, 1980,
                                                                                  1985, 1990, 1995, 2000, 2005, 
                                                                                  2010, 2015)
  ))  %>%  # 5-year birth Cohorts))
  group_by(country_id, cohort_5) %>%
  summarise(v2x_polyarchy_5 = mean.new(v2x_polyarchy), 
            v2xca_academ_5 = mean.new(v2xca_academ), 
            e_gdppc_5 = mean.new(e_gdppc),
            e_gdppc_5 = mean.new(e_pop), 
            autocracy_max = max.new(autocracy))


vdem_cohort_10 <- vdem %>%
  mutate(cohort_10 = cut(vdem$year, seq(1885, 2015, by = 10), right = F, labels = c(1885, 1895, 1905,
                                                                                    1915, 1925,  
                                                                                    1935, 1945, 1955,
                                                                                    1965, 1975, 
                                                                                    1985, 1995, 2005)
  ))  %>%  # 5-year birth Cohorts))
  group_by(country_id, cohort_10) %>%
  summarise(v2x_polyarchy_10 = mean.new(v2x_polyarchy), 
            v2xca_academ_10 = mean.new(v2xca_academ), 
            e_gdppc_10 = mean.new(e_gdppc),
            e_gdppc_10 = mean.new(e_pop), 
            autocracy_max = max.new(autocracy))
           

vdem_cohort_5$cohortmatch5_20 <- as.numeric(as.character(vdem_cohort_5$cohort_5))
vdem_cohort_10$cohortmatch10_20 <- as.numeric(as.character(vdem_cohort_10$cohort_10))


saveRDS(vdem, file = "data/vdem_data.rds")
saveRDS(vdem_cohort_5, file = "data/vdem_cohort_5.rds")
saveRDS(vdem_cohort_10, file = "data/vdem_cohort_10.rds")

  

